package com.fuli.app;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfigure implements WebMvcConfigurer {
    @Value("${upload.image.path}")
    private String imagePath;
    @Value("${upload.image.dir}")
    private String imageDir;
    @Value("${upload.image.trash}")
    private String imageTrash;

    /**
     * 添加外部静态资源映射
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(imagePath + "**").addResourceLocations("file:" + imagePath);
        registry.addResourceHandler("/upload/image/**").addResourceLocations("file:" + "C:\\FuLiXueTang\\upload\\image\\");
    }


    /**
     * 添加跨域请求支持
     *
     * @param registry
     */

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }
}
